import javax.swing.JDialog;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.net.InetAddress;

/**
 * A JDialog serving as a graphical extension to remove
 * a Webserver from Webster.
 *
 * @author Marlon Etheredge
 * @author <a href="mailto:m.etheredge@gmail.com">m.etheredge@gmail.com</a>
 * @author <a href="http://www.foxage.nl/">http://www.foxage.nl</a>
 */
public class RemoveServerDialog extends JDialog {
    
    /** Parent */
    private WebsterGUI gui;
    
    //Labels
    private JLabel serversLabel;
    
    //Fields
    private JComboBox servers;

    private JButton removeButton;
    
    /**
     * Construct a new RemoveServerDialog with frame as it's parent.
     *
     * @param frame The parent JFrame.
     */
    public RemoveServerDialog(WebsterGUI gui) {
        super((JFrame)gui);
        this.gui = gui;
        this.addComponents();
    }
    
    /**
     * Adds a components to this instance of RemoveServerDialog.
     */
    private void addComponents() {
        getContentPane().setLayout(null);
        this.setResizable(false);
        
        this.serversLabel = new JLabel("Servers");
        this.serversLabel.setBounds(10, 12, 157, 22);
        this.getContentPane().add(this.serversLabel);

        int serversSize = Webster.getWebservers().size();
        Object[] servers = new Object[serversSize];
        Webster.getWebservers().toArray(servers);

        DefaultComboBoxModel serversModel = new DefaultComboBoxModel(servers);
        this.servers = new JComboBox();
        this.servers.setModel(serversModel);
        this.servers.setBounds(146, 12, 157, 22);
        this.getContentPane().add(this.servers);

        this.removeButton = new JButton("Remove server");
        this.removeButton.setBounds(10, 46, 157, 22);
        this.getContentPane().add(removeButton);
        this.removeButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    Webster.removeWebserver((Webserver) RemoveServerDialog.this.servers.getSelectedItem());
                    RemoveServerDialog.this.dispose();        
                    JOptionPane.showMessageDialog(RemoveServerDialog.this.gui, "Removed webserver");
                    RemoveServerDialog.this.gui.updateServerTree();
                }
        });

        this.pack();

        this.setSize(325, 110);
        this.setVisible(true);
        this.setTitle("Remove a server");
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
}